import com.mysql.jdbc.exceptions.jdbc4.MySQLDataException;
import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * user:likai
 * Date:2022-11-30
 * Time:15:57
 */
public class Test {
    public static void main(String[] args) throws SQLException {
        //创建数据源
        DataSource dataSource=new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("lkh201216");
        //创建连接
        Connection connection=dataSource.getConnection();
        //
        Scanner scanner=new Scanner(System.in);
        String sql="select * from student where id=2";
        //int id=scanner.nextInt();
        //String name=scanner.next();
        //预处理
        PreparedStatement preparedStatement= connection.prepareStatement(sql);
//        preparedStatement.setInt(1,id);
//        preparedStatement.setString(2,name);
//        System.out.println("sql:"+preparedStatement);
//        //执行
//        int ret= preparedStatement.executeUpdate();
        ResultSet resultSet=preparedStatement.executeQuery();
        while(resultSet.next()){
            int id1=resultSet.getInt("id");
            String name1=resultSet.getString("name");
            System.out.println(id1+" "+name1);

        }
        //关闭
        resultSet.close();
        preparedStatement.close();
        connection.close();
    }
}
